/**
 * 
 */
package com.zznode.client.wlanpolling.login;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.method.NumberKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zznode.client.wlanpolling.R;
import com.zznode.client.wlanpolling.utils.WLanPollingUtils;

/**
 * @author Pink
 *
 */
public class NetworkSettingActivity extends Activity {

	
	private EditText etServerAddress;
	private EditText etPort;
	
	private String serverAddress;
	private String port;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.networksetting);
		
		SharedPreferences preferences = getSharedPreferences(WLanPollingUtils.SYSTEM_SETTING, 0);
		serverAddress = preferences.getString(WLanPollingUtils.KEY_ADDRESS, WLanPollingUtils.DEFAULT_SERVEADDRESS);
		port = preferences.getString(WLanPollingUtils.KEY_PORT, WLanPollingUtils.DEFAULT_PORT);
		
		bindEvent();
	}
	
	private void bindEvent() {
		etServerAddress = (EditText)findViewById(R.id.serveraddress);
		etServerAddress.setKeyListener(new NumberKeyListener() {

			@Override
			public int getInputType() {
				return android.text.InputType.TYPE_CLASS_TEXT;
			}

			@Override
			protected char[] getAcceptedChars() {
				return new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
				'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
				'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'.', '/', ':'
				};
			}
			
		});
		
		etPort = (EditText)findViewById(R.id.port);
		etServerAddress.setText(serverAddress);
		etPort.setText(port);
		
		Button btnSave = (Button)findViewById(R.id.btnSaveSetting);
		
		btnSave.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(checkInput()) {
					saveSetting();
					Toast.makeText(NetworkSettingActivity.this, 
							"保存设置成功!", Toast.LENGTH_LONG).show();
				}
				return;
			}
		});
	}
	
	private void saveSetting() {
		SharedPreferences settings = getSharedPreferences(WLanPollingUtils.SYSTEM_SETTING, 0);  
		SharedPreferences.Editor editor = settings.edit();  
		
		editor.putString(WLanPollingUtils.KEY_ADDRESS, etServerAddress.getText().toString());  
		editor.putString(WLanPollingUtils.KEY_PORT, etPort.getText().toString());
		
		editor.commit();
		
		WLanPollingUtils.serverAddress = etServerAddress.getText().toString();
		WLanPollingUtils.port = etPort.getText().toString();
	}
	
	private boolean checkInput() {
		String port = etPort.getText().toString();

		if(port != null) {
			try {
				int i  = Integer.parseInt(port);
				if(i >= 65535) {
					throw new Exception("Port number is wrong!");
				}
			} catch(Exception e) {
				Toast.makeText(this, "端口必须为数字且小于65535", Toast.LENGTH_LONG).show();
				return false;
			}
		}
		
		String address =  etServerAddress.getText().toString();
		if(address != null) {
			try {
				if(!address.startsWith("http://")) {
					throw new Exception("地址必须以http://开始");
				}
				if(address.length() > 254) {
					throw new Exception("地址长度不能超过254字符");
				}
			} catch(Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
				return false;
			}
		}
		
		return true;
	}
}
